package cn.zyq.ykb.service.config.impl;

import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.enums.core.ResponseCodeEnum;
import cn.zyq.ykb.system.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.config.ConfigBrokerageMapper;
import cn.zyq.ykb.beans.config.ConfigBrokerage;
import cn.zyq.ykb.service.config.ConfigBrokerageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 配置-返佣金Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-29
 */
@Service
public class ConfigBrokerageServiceImpl  extends BaseService implements ConfigBrokerageService {

    @Autowired
    private ConfigBrokerageMapper configBrokerageMapper;

    /**
     * 查询配置-返佣金
     *
     * @param configBrokerageID 配置-返佣金主键
     * @return 配置-返佣金
     */
    @Override
    public ConfigBrokerage selectConfigBrokerageByConfigBrokerageID(Integer configBrokerageID) {
        ConfigBrokerage configBrokerage = configBrokerageMapper.selectByPrimaryKey(configBrokerageID);
        if (configBrokerage.getDeleted() == 1) {
            return null;
        }
        return configBrokerage;
    }

    /**
     * 查询配置-返佣金
     *
     * @param serviceID 业务ID
     * @return 配置-返佣金
     */
    @Override
    public ConfigBrokerage selectConfigBrokerageByServiceID(String serviceID) {
        ConfigBrokerage param = new ConfigBrokerage();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configBrokerageMapper.selectOne(param);
    }

    /**
     * 查询配置-返佣金列表
     *
     * @param pageParam
     * @return 配置-返佣金
     */
    @Override
    public PageInfo<ConfigBrokerage> selectConfigBrokerageList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        pageParam.getKv().put("type", 1);
        List<ConfigBrokerage> list = this.configBrokerageMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询配置-返佣金列表
     *
     * @param param
     * @return 配置-返佣金
     */
    @Override
    public List<ConfigBrokerage> selectConfigBrokerageList(ConfigBrokerage param) {
        param.setDeleted(0);
        return this.configBrokerageMapper.select(param);
    }
    /**
     * 新增配置-返佣金
     *
     * @param param 配置-返佣金
     * @return 结果
     */
    @Override
    public int insertConfigBrokerage(ConfigBrokerage param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        param.setType(1);
        return this.configBrokerageMapper.insertSelective(param);
    }

    /**
     * 修改配置-返佣金
     *
     * @param param 配置-返佣金
     * @return 结果
     */
    @Override
    public int updateConfigBrokerage(ConfigBrokerage param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configBrokerageMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除配置-返佣金
     *
     * @param configBrokerageIDs 需要删除的配置-返佣金主键
     * @return 结果
     */
    @Override
    public int deleteConfigBrokerageByConfigBrokerageIDs(Integer[] configBrokerageIDs) {
        int i = 0;
        for (Integer configBrokerageID : configBrokerageIDs) {
            i += this.deleteConfigBrokerageByConfigBrokerageID(configBrokerageID);
        }
        return i;
    }

    /**
     * 删除配置-返佣金信息
     *
     * @param configBrokerageID 配置-返佣金主键
     * @return 结果
     */
    @Override
    public int deleteConfigBrokerageByConfigBrokerageID(Integer configBrokerageID) {
        ConfigBrokerage configBrokerage = this.selectConfigBrokerageByConfigBrokerageID(configBrokerageID);
        if (configBrokerage == null) {
            return 0;
        } else {
            ConfigBrokerage storeInfo = new ConfigBrokerage();
            storeInfo.setConfigBrokerageID(configBrokerageID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configBrokerageMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public void add(User loginUser) {
        ConfigBrokerage configBrokerage = new ConfigBrokerage();
        configBrokerage.setUserID(1);
        configBrokerage.setType(1);
        ConfigBrokerage configBrokerage1 = this.configBrokerageMapper.selectOne(configBrokerage);
        ConfigBrokerage insert = new ConfigBrokerage();
        insert.setUserID(loginUser.getUserID());
        insert.setType(0);
        insert.setDefaultUserProportion(configBrokerage1.getDefaultUserProportion());
        insert.setDefaultCaptainProportion(configBrokerage1.getDefaultCaptainProportion());
        insert.setCaptainMin(configBrokerage1.getCaptainMin());
        insert.setCaptainProportion(configBrokerage1.getCaptainProportion());
        insert.setUserMin(configBrokerage1.getUserMin());
        insert.setUserMax(configBrokerage1.getUserMax());
        this.setCreateBaseData(insert,loginUser);
        int i = this.configBrokerageMapper.insertSelective(insert);
        if (i == 0){
            throw new BusinessException(ResponseCodeEnum.ERROR,"新增服务商佣金表失败");
        }
    }
}
